package n21;

public class Main1 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode root = new ListNode(-1);
        ListNode prev = root;
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                prev.next = list1;
                list1 = list1.next;
            } else {
                prev.next = list2;
                list2 = list2.next;
            }
            prev = prev.next;
        }
        prev.next = list1==null?list2:list1;
        return root.next;
    }

    public static void main(String[] args) {
        Main1 main = new Main1();
        // ListNode list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
        // ListNode list2 = new ListNode(1, new ListNode(3, new ListNode(4)));
        ListNode list1 = new ListNode(1, new ListNode(5));
        ListNode list2 = new ListNode(1, new ListNode(2, new ListNode(4)));
        ListNode listNode = main.mergeTwoLists(list1, list2);
        System.out.println(listNode);

    }
}
